//
//  AppDelegate.swift
//  SinaWeiBo
//
//  Created by immncjykeny on 2017/4/21.
//  Copyright © 2017年 immncjykeny. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
       
        
        
       setupAdditions()
        
        window = UIWindow()
        
        window?.backgroundColor = UIColor.white
        
        window?.rootViewController = HMMainViewController()
        
        window?.makeKeyAndVisible()
        
        loadAPPInfo()
        
        return true
    }

   //3D touch 触发方法
    func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void){
        
        var sourceButtonTitle :String?
        
        switch shortcutItem.localizedTitle {
        case "3D Touch 测试按钮":
            sourceButtonTitle = "3D Touch 测试按钮"
            break
        case "出来吧，小火龙！":
            sourceButtonTitle = "出来吧，小火龙！"
            break
            case "一起分享精彩每一天!":
            sourceButtonTitle = "一起分享精彩每一天"
            break
        default:
            break
        
       }
        if let trysourceButtonTitle = sourceButtonTitle {
            let alert = UIAlertController(title: nil, message: trysourceButtonTitle, preferredStyle: .actionSheet)
            alert.addAction(UIAlertAction(title: "知道啦", style: .cancel, handler: nil))
           window?.rootViewController?.present(alert, animated: true, completion: nil)
        }

    }
    
}

//设置额外信息
extension AppDelegate{
    
     func setupAdditions(){
        //设置SVProgressHUD 解除时间最小
        SVProgressHUD.setMinimumDismissTimeInterval(1.0)
        
        //设置AFN网络加载指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        //设置应用授权
        //这个是iOS 10.0之后的授权方式
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.carPlay,.sound]) { (success, error) in
            print("授权" + "\(success ? "成功" : "失败")")
        }
        
        
    }
}
//模拟延迟加载
extension AppDelegate{
    
    fileprivate func loadAPPInfo(){
        
        //模拟延迟加载
        DispatchQueue.global().async {
            
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
            
            let data = NSData(contentsOf: url!)
            
            //获取本地存储 也就是沙盒 或者磁盘
            //这个是沙盒的document目录存储
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            
            let jsonPath = (docDir as NSString).appendingPathComponent("main.json")
            
            print("应用程序存储在\(jsonPath)")
                //也是plist存储
           data?.write(toFile: jsonPath, atomically: true)
            
            
            
        }
        
    }
}

